<template>
	<view class="new-users copy-data">
		<view class="bg"></view>
		<view class="head">
			<view class="user-card">
				<view>
					<image :src="imageItem.img"  class="bg"></image>
				</view>
				<view class="user-info">
					<!-- <view class="headwear" @click="toLogin"> -->
						<!-- <open-data type="userAvatarUrl"></open-data> -->
						<!-- <view class="userinfo-avatar">
							<image :src="userInfo.avatarUrl" mode="scaleToFill"></image>					
						</view> -->
						<!-- <button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar"> -->
						  <!-- <image class="avatar" :src="userInfo.avatarUrl"></image> -->
						<!-- </button> -->
					<!-- </view> -->
					<!-- <view class="info" @click="toLogin">
						<view class="name" style="font-size: 30rpx;margin-top: 15px;">
							{{hasLogin?decodeURIComponent(userInfo.nickName):"请点击授权登录"}}
						</view>
						
						<block v-if="hasLogin">
							<button type="default" open-type="getPhoneNumber" plain="true" @getphonenumber="getMobile"
								v-if="!userInfo.mobile"
								style="color: #fff;border: none;">绑定手机号</button>
							<view style="color: #fff;font-size: 12px;padding-top: 5px;" v-else>
								{{userInfo.mobile}}
							</view>
						</block>
					</view> -->
				</view>
			</view>
		</view>
		<view class="main">
			<!-- <view class="order-wrapper">
				<view class="order-hd flex">
					<view class="left" style="float: left;font-weight: bold;">订单中心</view>
					<view style="float: right;font-size: 13px;color: #666;" hover-class="none"
						@click="navTo('/pages/order/list?current=0','',true)">
						查看全部
						<text class="mix-icon icon-you" style="font-size: 14px;"></text>
					</view>
				</view>
				<view class="order-bd">
					<block v-for="(item,index) in orderMenu" :key="index">
						<view class="order-item" hover-class="none" @click="navTo(item.url,'',true)">
							<view class="pic">
								<image :src="item.img" mode=""></image>
								<text class="order-status-num" v-if="item.num > 0">{{ item.num }}</text>
							</view>
							<view class="txt">{{item.title}}</view>
						</view>
					</block>
				</view>
			</view> -->
			<!-- 会员菜单 -->
			<view style="margin-top: 20rpx; border-radius: 6rpx; overflow: hidden;">
				<view class="title-box">我的服务</view>
				<view class="user-menus">
					<view class="item" @click="goTo('/pages/my/donationRecord')">
						<image src="../../static/wdye.png"></image>
						<text>我的供养</text>
					</view>
					<view class="item" @click="goTo('/pages/my/activityList')">
						<image src="../../static/wdsc.png"></image>
						<text>我的活动</text>
					</view>
					<!-- <view class="item" @click="navTo('/pages/address/list','',true)">
						<image src="../../static/shdz.png"></image>
						<text>地址管理</text>
					</view> -->
					<view class="item" @click="callPhone('13970804681')">
						<image src="../../static/lxkf.png"></image>
						<text>联系寺院</text>
					</view>
					<view class="item" @click="goPosition(location)">
						<image src="../../static/yjfk.png"></image>
						<text>地图导航</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import helper from '../../common/js/helper.js';
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				orderMenu: [{
						img: '/static/order1.png',
						title: '待付款',
						url: '/pages/order/list?current=0',
						num: 0
					},
					{
						img: '/static/order2.png',
						title: '待发货',
						url: '/pages/order/list?current=1',
						num: 0
					},
					{
						img: '/static/order3.png',
						title: '待收货',
						url: '/pages/order/list?current=2',
						num: 0
					},
					{
						img: '/static/order4.png',
						title: '已收货',
						url: '/pages/order/list?current=3',
						num: 0
					},
					{
						img: '/static/order5.png',
						title: '售后/退款',
						url: '/pages/order/list?current=4',
						num: 0
					},
				],
				
				location:{
				},
				imageItem:{}
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		onLoad(options) {
			if (options.q) {
				//扫码
				//推广用户的身份id
				var uid = decodeURIComponent(options.q).split('uid=')[1];
				uni.setStorageSync('uid', uid);
			}
			if (options.uid) {
				//转发
				uni.setStorageSync('uid', options.uid);
			}
			let that = this;
			uni.login({
				success(res) {
					if (res.errMsg == 'login:ok') {
						that.$http.get('/wechat/getOpenId', {
							loading: 'hide',
							code: res.code
						}).then((ret) => {
							console.log(ret)
							uni.setStorageSync('session_key', ret.data.result.session_key);
							uni.setStorageSync('openid', ret.data.result.openid);
							that.wxLogin()
						});
					} else {
						uni.showModal({
							content: res.errMsg,
							showCancel: false
						})
					}
				}
			})
			this.$http.get('/sys/sysDepart/api/getDepartName', {
				'orgCode':getApp().globalData.orgCode
			}).then((res) => {
				console.log(res)
				this.location = res.data.result
			})
			//加载banner
			this.$http.get('/temple.appearance/templeAppearanceBanner/api/list', {
				'location':'my'
			}).then((res) => {
				var bannerList = res.data.result.records;
				if(bannerList.length > 0){
					this.imageItem = bannerList[0];
				}
			})
		},
		async onShow() {
			if (this.hasLogin) {
				await this.getUserInfo();
				await this.getRed();
				// this.$http.post('Goods/getGoodsBrowse', {
				// 	loading: 'hide'
				// }).then((res) => {
				// 	this.browse = res.data.data;
				// });

			}
		},
		
		methods: {
			...mapMutations(['login', 'logout']),
			toLogin() {
				if (!this.hasLogin) {
					// this.navTo('/pages/login/login');
					this.wxLogin();
				}
			},
			loginOut() {
				uni.removeStorageSync("userInfo");
				this.logout();
			},
			//绑定手机号
			getMobile(e) {
				var that = this;
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					uni.checkSession({
						success(check) {
							if (check.errMsg == 'checkSession:ok') {
								that.$http.get('/wechat/decryptMobile', {
									id: uni.getStorageSync('userInfo').id,
									session_key: uni.getStorageSync('session_key'),
									encryptedData: e.detail.encryptedData,
									iv: e.detail.iv
								}).then((res) => {
									if (res.data.message.indexOf("error")==-1) {
										uni.setStorageSync("userInfo", res.data.result);
										that.$store.commit('login', res.data.result);
										
										that.toast("获取手机号成功");
										setTimeout(function() {
											uni.navigateBack();
										}, 1000);
									}
								})
							} else {
								that.toast('登录状态已经过期');
							}
						}
					})
				}
			},
			wxLogin() {
				let that = this;
				that.$http.get('/wechat/wechatLogin', {
					openId: uni.getStorageSync('openid'),
					parentId: uni.getStorageSync('uid') ? uni.getStorageSync('uid') : 0,
					nickName:'',//encodeURIComponent(infoRes.userInfo.nickName),
					avatarUrl:'', //infoRes.userInfo.avatarUrl,
					sex:'' //infoRes.userInfo.gender,
				}).then((res) => {
					console.log(res)
					if (res.data.code == 200) {
						uni.removeStorageSync('uid');
						uni.setStorageSync("userInfo", res.data.result);
						that.$store.commit('login', res.data.result);
						// that.toast("登录成功");
						// setTimeout(function() {
						// 	uni.navigateBack();
						// }, 1000);
					}
				});
			},
			async getRed() {
				this.$http.post('order/getred', {
					loading: 'hide',
				}).then((res) => {
					this.orderMenu[0].num = res.data.data.daifukuan;
					this.orderMenu[1].num = res.data.data.daifahuo;
					this.orderMenu[2].num = res.data.data.yifahuo;
					this.orderMenu[3].num = res.data.data.yishouhuo;
					this.orderMenu[4].num = res.data.data.tuikuan;
				})
			},
			goTo(url){
				// if (!this.hasLogin) {
				// 	this.wxLogin()
				// }else{
					uni.navigateTo({
						url: url 
					})
				// }
			},
			callPhone(phone){
				uni.makePhoneCall({
					phoneNumber:phone
				})
			},
			goPosition: function (item) {
			    wx.getLocation({ //获取当前经纬度
			      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
			      success: function (res) {
			        wx.openLocation({ //​使用微信内置地图查看位置。
			          latitude: parseFloat(item.latitude), //要去的纬度-地址
			          longitude: parseFloat(item.longitude), //要去的经度-地址
			          name: item.departName,
			          address: item.address
			        })
			      }
			    })
			  }
		}
	}
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss">
	button {
		border-radius: 0rpx;
		padding: 0rpx;
		margin: 0rpx;
		display: inline-block;
		line-height: 1;
		// color: #fff !important;
		font-size: 12px;
		// border: none !important;
		text-align: left;
	}

	button::after {
		/*button的边框样式是通过::after方式实现的*/
		border: none;
	}

	.button-hover {
		/*点击后样式*/
		color: #000;
		background-color: rgb(247, 247, 247);
	}

	.bg {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 320rpx;
		// background-image: url('~@/static/user_bg.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
		z-index: 9999;
	}

	.new-users {
		.head {
			.user-card {
				position: relative;
				width: 100%;
				height: 220rpx;
				margin: 0 auto;
				padding: 35rpx 28rpx;
				background: rgb(236, 78, 65);

				&::after {
					position: absolute;
					left: 0;
					right: 0;
					bottom: -98rpx;
					content: '';
					height: 100rpx;
					width: 100%;
					// border-radius: 0 0 50% 50%;
					background: rgb(236, 78, 65);
				}

				.user-info {
					z-index: 999999;
					position: relative;
					display: flex;

					.headwear {
						overflow: hidden;
						display: block;
						width: 130rpx;
						height: 130rpx;
						margin: 20rpx 0 50rpx 20rpx;
						border-radius: 50%;
						border: 2px solid #fafafa;
						box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
					}

					.bntImg {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						text-align: center;
						line-height: 120rpx;
						background-color: unset;
						position: relative;

						.avatarName {
							font-size: 16rpx;
							color: #fff;
							text-align: center;
							background-color: rgba(0, 0, 0, 0.6);
							height: 37rpx;
							line-height: 37rpx;
							position: absolute;
							bottom: 0;
							left: 0;
							width: 100%;
						}
					}

					.avatar {
						display: block;
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						margin: 0 auto;
					}

					.info {
						margin-left: 20rpx;
						padding: 15rpx 0;

						.name {
							display: flex;
							align-items: center;
							color: #fff;
							font-size: 31rpx;

							.vip-txt {
								margin-left: 10rpx;
								font-size: 20rpx;
								border: 1px solid #fff;
								border-radius: 20rpx;
								padding: 0 10rpx;
							}

							.vip {
								width: 28rpx;
								height: 28rpx;
								margin-left: 14rpx;
							}

							.svip {
								width: 78rpx;
								height: 30rpx;
								margin-left: 12rpx;
							}
						}

						.num {
							display: flex;
							align-items: center;
							font-size: 26rpx;
							color: rgba(255, 255, 255, 0.6);

							image {
								width: 22rpx;
								height: 23rpx;
								margin-left: 20rpx;
							}
						}
					}
				}

				.sign {
					z-index: 200;
					position: absolute;
					right: -12rpx;
					top: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 120rpx;
					height: 60rpx;
					background: linear-gradient(90deg, rgba(255, 225, 87, 1) 0%, rgba(238, 193, 15, 1) 100%);
					border-radius: 29rpx 4rpx 4rpx 29rpx;
					color: #282828;
					font-size: 28rpx;
					font-weight: bold;
				}
			}
		}

		.main {
			position: relative;
			padding: 0 20rpx;
			z-index: 99999;

			.num-wrapper {
				z-index: 30;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 20rpx;
				height: 140rpx;
				border-radius: 6rpx;
				// padding: 0 47rpx;
				color: #333;
				background: #fff;

				.num-item {
					width: 33.33%;

					text-align: center;

					.num {
						font-size: 32rpx;
					}

					.txt {
						margin-top: 8rpx;
						font-size: 26rpx;
						color: #aaa;
					}
				}
			}

			.order-wrapper {
				background-color: #fff;
				border-radius: 6rpx;

				.order-hd {
					justify-content: space-between;
					padding: 20rpx 28rpx;
					margin-top: 20rpx;
					padding-bottom: 30px;
					border-bottom: 1px solid #F5F5F5;
					font-size: 30rpx;
					color: #282828;

					.right {
						align-items: center;
						color: #666666;
						font-size: 26rpx;

						.icon-xiangyou {
							margin-left: 5rpx;
							margin-top: 4rpx;
							font-size: 26rpx;
						}
					}
				}

				.order-bd {
					display: flex;

					.order-item {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 20%;
						height: 160rpx;

						.pic {
							position: relative;
							text-align: center;

							image {
								width: 49rpx;
								height: 42rpx;
							}
						}

						.txt {
							margin-top: 15rpx;
							font-size: 26rpx;
							color: #333;
						}
					}
				}
			}
		}

		.slider-wrapper {
			margin: 20rpx 0;
			height: 130rpx;

			swiper,
			swiper-item {
				height: 100%;
			}

			image {
				width: 100%;
				height: 130rpx;
			}
		}

		.user-menus {
			background-color: #fff;
			display: flex;
			flex-wrap: wrap;
			padding: 20rpx 0;

			.item {
				position: relative;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;
				width: 25%;
				height: 134rpx;
				font-size: 24rpx;

				image {
					width: 45rpx;
					height: 45rpx;
					margin-bottom: 15rpx;
				}
			}

			button {
				font-size: 28rpx;
			}

			.item text {
				font-size: 14px;
			}
		}

		.phone {
			color: #fff;
			font-size: 12px;
			margin-top: 5px;
		}

		.order-status-num {
			background-color: #ff0000;
			border-radius: 38px;
			position: absolute;
			right: -10rpx;
			top: -10rpx;
			font-size: 20rpx;
			width: 14px;
			height: 14px;
			border: 1px solid #ff0000;
			line-height: 13px;
			color: #fff;
			padding-left: 1px;
		}

		.support {
			width: 219rpx;
			height: 74rpx;
			margin: 54rpx auto;
			display: block;
		}
	}

	.title-box {
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 30rpx;
		border-bottom: 1px dashed #eee;
		background-color: #fff;
	}
</style>
